package com.bdy.lm.taximanager.thread;

import android.os.Message;
import android.util.Log;

import com.bdy.lm.taximanager.app.MyApp;
import com.bdy.lm.taximanager.http.HttpJson;
import com.bdy.lm.taximanager.model.MessagePackaging;
import com.bdy.lm.taximanager.model.ServerCommandEvent;
import com.bdy.lm.taximanager.util.JsonUtils;

import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/8/14.
 */
public class LocationReportThread extends Thread {

    private MessagePackaging messagePackaging = new MessagePackaging(111);
    private Map<String, Object> locationMessage;
    private HttpJson httpJson = new HttpJson();
    private String decode, encode;
    private String flag = "位置线程";
    private boolean beginLocationReport = true;
    private Message message;
    private int tracePeriod = 1000, traceDistance, traceContinueDistance, traceContinueTime;
    private double startla, startlo, endla, endlo, d = 0, ed = 0;

    public void stopLocationReport() {
        beginLocationReport = false;
    }

    public void beginLocationReport() {
        beginLocationReport = true;
    }

    public void run() {
        EventBus.getDefault().register(this);
        while (MyApp.threadRun) {
            if (beginLocationReport) {
                if ((message != null) && (message.what == 521)) {
                    stopLocationReport();
                }
                delay(5000);
                locationMessage = messagePackaging.getLocationMessagePackageMap();
                httpJson.HttpJsonPost(JsonUtils.mapToJsonObject(locationMessage), MyApp.serverUrl);

                //System.out.println("位置" + jsonUtils.mapToJsonObject(locationMessage));
                //encode = AES.encode(jsonUtils.mapToJsonObject(locationMessage).toString());
                //System.out.println("加密"+encode);
                //System.out.println("解密"+AES.decode(encode));
                Log.d(flag, "位置汇报");
            } else {
                int traceProperty = message.getData().getInt("traceProperty");
                if (traceProperty == 0x00) {
                    tracePeriod = message.getData().getInt("tracePeriod");
                    traceContinueTime = message.getData().getInt("traceContinueTime");
                    while (traceContinueTime - tracePeriod >= 0) {
                        delay(tracePeriod * 1000);
                        locationMessage = messagePackaging.getLocationMessagePackageMap();
                        //httpJson.HttpJsonPost(jsonUtils.mapToJsonObject(locationMessage), MyApp.locationUrl);
                    }
                    beginLocationReport();
                } else if (traceProperty == 0x11) {
                    traceDistance = message.getData().getInt("traceDistance");
                    traceContinueDistance = message.getData().getInt("traceContinueDistance");
                    endla = MyApp.locationMessageBody.getLatitude();
                    endlo = MyApp.locationMessageBody.getLongitude();
                    while (traceContinueDistance - traceDistance >= 0) {
                        distance();
                        if (d >= traceDistance) {
                            locationMessage = messagePackaging.getLocationMessagePackageMap();
                            //httpJson.HttpJsonPost(jsonUtils.mapToJsonObject(locationMessage), MyApp.locationUrl);
                            d = 0;
                        }
                    }
                    beginLocationReport();
                } else if (traceProperty == 0x01) {
                    tracePeriod = message.getData().getInt("tracePeriod");
                    traceContinueDistance = message.getData().getInt("traceContinueDistance");
                    endla = MyApp.locationMessageBody.getLatitude();
                    endlo = MyApp.locationMessageBody.getLongitude();
                    while (traceContinueDistance <= d) {
                        distance();
                        delay(tracePeriod * 1000);
                        locationMessage = messagePackaging.getLocationMessagePackageMap();
                        //httpJson.HttpJsonPost(jsonUtils.mapToJsonObject(locationMessage), MyApp.locationUrl);
                    }
                    beginLocationReport();
                } else if (traceProperty == 0x11) {
                    traceDistance = message.getData().getInt("traceDistance");
                    traceContinueTime = message.getData().getInt("traceContinueTime");
                    long time = getTime();
                    while (getTime() - time <= traceContinueTime) {//都是秒
                        distance();
                        if (d >= traceDistance) {
                            locationMessage = messagePackaging.getLocationMessagePackageMap();
                            //httpJson.HttpJsonPost(jsonUtils.mapToJsonObject(locationMessage), MyApp.locationUrl);
                            d = 0;
                        }
                    }
                    beginLocationReport();
                } else if (traceProperty == 0xff) {
                    beginLocationReport();
                }
                Log.d(flag, "位置汇报停止,处理指令");
            }
        }
    }

    public void onEventAsync(ServerCommandEvent serverCommandEvent) {
        this.message = serverCommandEvent.getMessage();
    }

    void delay(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
        }
    }

    void distance() {
        startla = endla;
        startlo = endlo;
        endla = MyApp.locationMessageBody.getLatitude();
        endlo = MyApp.locationMessageBody.getLongitude();
        double lat1 = (Math.PI / 180) * startla;
        double lon1 = (Math.PI / 180) * startlo;
        double lat2 = (Math.PI / 180) * endla;
        double lon2 = (Math.PI / 180) * endlo;
        double R = 6371;
        ed = 1000 * Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;
        d = d + ed;
    }

    //获取Unix时间戳
    long getTime() {
        return System.currentTimeMillis() / 1000;
    }
}
